package com.code.zjm.activity;

import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.code.zjm.R;
import com.code.zjm.entity.User;
import com.code.zjm.helper.DBHelper;

/**
 * Created by ZJM on 2017/4/27.
 * 添加联系人界面
 */

public class AddUserActivity extends BaseUserActivity {
    private final static String TAG = AddUserActivity.class.getSimpleName();
    private EditText etUsername, etCompany, etPosition, etMobilePhone, etFamilyPhone,
            etOfficePhone, etOtherContract, etEmail, etAddress, etRemark, etZipCode;

    @Override
    protected void initView() {
        etUsername = (EditText) findViewById(R.id.et_username);
        etCompany = (EditText) findViewById(R.id.et_company);
        etPosition = (EditText) findViewById(R.id.et_position);
        etMobilePhone = (EditText) findViewById(R.id.et_mobile_phone);
        etFamilyPhone = (EditText) findViewById(R.id.et_family_phone);
        etOfficePhone = (EditText) findViewById(R.id.et_office_phone);
        etOtherContract = (EditText) findViewById(R.id.et_other_contract);
        etEmail = (EditText) findViewById(R.id.et_email);
        etAddress = (EditText) findViewById(R.id.et_address);
        etRemark = (EditText) findViewById(R.id.et_remark);
        etZipCode = (EditText) findViewById(R.id.et_zip_code);

        userImage = (ImageView) findViewById(R.id.iv_user);
        userImage.setOnClickListener(this);
    }

    @Override
    protected String getTAG() {
        return TAG;
    }

    @Override
    protected Object getContentView() {
        return R.layout.activity_add;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //实例化解析菜单对象
        MenuInflater inflater = getMenuInflater();
        //解析菜单资源文件，保存在 menu 中
        inflater.inflate(R.menu.add_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_ensure:
                //判断姓名是否为空
                String name = etUsername.getText().toString();
                if (name.trim().equals("")) {
                    Toast.makeText(AddUserActivity.this, "姓名不许为空", Toast.LENGTH_LONG).show();
                    return false;
                }
                //从表单上获取数据
                User user = new User();
                user.setUsername(name);
                user.setCompany(etCompany.getText().toString());
                user.setPosition(etPosition.getText().toString());
                user.setMobilePhone(etMobilePhone.getText().toString());
                user.setFamilyPhone(etFamilyPhone.getText().toString());
                user.setOfficePhone(etOfficePhone.getText().toString());
                user.setOtherContact(etOtherContract.getText().toString());
                user.setEmail(etEmail.getText().toString());
                user.setAddress(etAddress.getText().toString());
                user.setRemark(etRemark.getText().toString());
                user.setZipCode(etZipCode.getText().toString());
                //判断头像是否改变，若改变，则用当前的位置，若没有改变，则用前一回的位置
                if (imageChanged) {
                    user.setImageId(images[currentImagePosition % images.length]);
                } else {
                    user.setImageId(images[previousImagePosition % images.length]);
                }
                user.setPrivacy(privacy);
                //创建数据库帮助类
                DBHelper helper = new DBHelper(AddUserActivity.this);
                //打开数据库
                helper.openDatabase();
                //把user存储到数据库里
                long result = helper.insert(user);

                //通过结果来判断是否插入成功，若为1，则表示插入数据失败
                if (result == -1) {
                    Toast.makeText(AddUserActivity.this, "添加失败", Toast.LENGTH_LONG).show();
                }
                setTitle("用户添加成功!");
                //返回到上一个Activity，也就是Main.activity
                setResult(3);
                //销毁当前视图
                finish();
                break;
            case android.R.id.home://对应 ActionBar 的返回图标
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_user:
                loadImage();//为gallery装载图片
                initImageChooseDialog();//初始化imageChooseDialog
                imageChooseDialog.show();
        }
    }

    /**
     * 当退出的时候，回收资源
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(getTAG(), "---------------onDestroy is called in " + getTAG());
        if (iSwitcher != null) {
            iSwitcher = null;
        }
        if (gallery != null) {
            gallery = null;
        }
        if (imageChooseDialog != null) {
            imageChooseDialog = null;
        }
        if (imageChooseView != null) {
            imageChooseView = null;
        }
        if (userImage != null) {
            userImage = null;
        }

    }
}
